package com.psp.sp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.northend.util.StringUtil;
import com.psp.sp.cache.dao.UserCacheDao;
import com.psp.sp.controller.res.BaseResult;
import com.psp.sp.controller.res.ObjectResult;
import com.psp.sp.controller.res.bean.RUserBean;
import com.psp.sp.controller.springmvc.req.ApplyParam;
import com.psp.sp.controller.springmvc.req.AtivateEmailParam;
import com.psp.sp.controller.springmvc.req.CheckLoginUserParam;
import com.psp.sp.controller.springmvc.req.CheckRegUserParam;
import com.psp.sp.controller.springmvc.req.ConfirmFindPwdCodeParam;
import com.psp.sp.controller.springmvc.req.GetUserParam;
import com.psp.sp.controller.springmvc.req.LoginParam;
import com.psp.sp.controller.springmvc.req.RegisterParam;
import com.psp.sp.controller.springmvc.req.ResetPwdParam;
import com.psp.sp.controller.springmvc.req.SendAtivateEmailParam;
import com.psp.sp.controller.springmvc.req.SendFindPwdCodeParam;
import com.psp.sp.controller.springmvc.req.SendVCodeParam;
import com.psp.sp.controller.springmvc.req.UpdatePhoneParam;
import com.psp.sp.controller.springmvc.req.UpdateUserParam;
import com.psp.sp.model.Code;
import com.psp.sp.model.UserBean;
import com.psp.sp.service.UserService;
import com.psp.sp.service.exception.ServiceException;

@Component
public class UserController {
	Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	UserService userServiceImpl;
	@Autowired
	UserCacheDao userCacheImpl;

	public ObjectResult<RUserBean> register(RegisterParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();

		String userName = param.getUserName();
		String pwd = param.getPassword();
		String confirmPwd = param.getConfirmPwd();
		String vcode = param.getVcode();
		String device = param.getDevice();
		String imgCode = param.getImgCode();
		try {
			String sessionId = request.getSession().getId();
			UserBean user = null;
			if (StringUtil.isMobileNO(userName)) {
				user = userServiceImpl.regByPhone(sessionId, userName, pwd, confirmPwd, vcode, device, imgCode);
			} else if (StringUtil.isEmail(userName)) {
				user = userServiceImpl.regByEmail(sessionId, userName, pwd, confirmPwd, device, imgCode);
			}
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getUserName());
				result.setToken(sessionId);
				result.setData(data);
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult sendVCode(SendVCodeParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();

		String phone = param.getPhone();
		int type = NumUtil.toInt(param.getType(), 0);
		String icode = param.getVcode();
		try {
			boolean flag = userServiceImpl.sendVCode(icode, type, phone);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	public BaseResult activateEmail(AtivateEmailParam param, HttpServletRequest request, HttpServletResponse response) {
		String code = param.getActivateCode();
		BaseResult result = new BaseResult();
		try {
			boolean flag = userServiceImpl.activateEmail(code);
			if (flag) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public ObjectResult<RUserBean> login(LoginParam param, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();

		String userName = param.getUserName();
		String pwd = param.getPassword();
		String vcode = param.getVcode();
		try {
			String sessionId = request.getSession().getId();
			UserBean user = userServiceImpl.login(sessionId, userName, pwd, vcode);
			logger.info("login user is:" + user);
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getUserName());
				data.setEmail(user.getEmail());
				data.setGender(user.getGender());
				data.setHeadIcon(user.getHeadIcon());
				data.setIdno(user.getIdno());
				data.setPhone(user.getPhone());
				data.setState(user.getState());
				result.setToken(sessionId);
				result.setData(data);
				result.setFlag(true);
			} else {
				Code code = userCacheImpl.getLoginCode(user.getUid());
				if(code!=null) {
					result.setErrorCount(code.getNum());
				}
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult checkRegUser(CheckRegUserParam param, HttpServletRequest request, HttpServletResponse response) {
		String username = param.getUserName();
		BaseResult result = new BaseResult();
		try {
			UserBean user = userServiceImpl.existUser(1, username);
			if (user != null) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult checkLoginUser(CheckLoginUserParam param, HttpServletRequest request,
			HttpServletResponse response) {
		String username = param.getUserName();
		BaseResult result = new BaseResult();
		try {
			UserBean user = userServiceImpl.existUser(2, username);
			if (user != null) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult sendActivateEmail(SendAtivateEmailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		String email = param.getEmail();
		BaseResult result = new BaseResult();
		try {
			boolean flag = userServiceImpl.sendActivateEmail(email);
			if (flag) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public ObjectResult<RUserBean> getUser(GetUserParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();

		String UserId = (String) request.getAttribute("userId");
		try {
			UserBean user = userServiceImpl.getUser(UserId);
			logger.info("login user is:" + user);
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getUserName());
				data.setEmail(user.getEmail());
				data.setGender(user.getGender());
				data.setHeadIcon(user.getHeadIcon());
				data.setIdno(user.getIdno());
				data.setPhone(user.getPhone());
				data.setState(user.getState());
				result.setData(data);
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult updateUser(UpdateUserParam param, HttpServletRequest request, HttpServletResponse response) {
		int gender = NumUtil.toInt(param.getGender(), 1);// 默认男
		String headerIcon = param.getHeadIcon();
		String idNo = param.getIdNo();
		String Name = param.getName();
		String userId = (String) request.getAttribute("userId");
		BaseResult result = new BaseResult();
		try {
			boolean flag = userServiceImpl.updateUser(userId, Name, idNo, headerIcon, gender);
			if (flag) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult updatePhone(UpdatePhoneParam param, HttpServletRequest request, HttpServletResponse response) {
		return null;
	}


	public ObjectResult<RUserBean> sendFindPwdCode(SendFindPwdCodeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();

		String userName = param.getAccount();
		String code = param.getVcode();
		try {
			UserBean user = userServiceImpl.sendFindPwdCode(userName, code);
			
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getUserName());
				data.setEmail(user.getEmail());
				data.setPhone(user.getPhone());
				result.setData(data);
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public ObjectResult<RUserBean> confirmFindPwdCode(ConfirmFindPwdCodeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();
		String account = param.getAccount();
		String vcode = param.getVcode();
		try {
			UserBean user = userServiceImpl.confirmFindPwdCode(account, vcode);
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getUserName());
				data.setEmail(user.getEmail());
				data.setPhone(user.getPhone());
				result.setData(data);
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult resetPwd(ResetPwdParam param, HttpServletRequest request, HttpServletResponse response) {
		String phone = param.getAccount();
		String pwd = param.getPwd();
		String confirmPwd = param.getConformPwd();
		String vcode = param.getVcode();
		BaseResult result = new BaseResult();
		try {
			boolean flag = userServiceImpl.resetPwd(phone, vcode, pwd, confirmPwd);
			if (flag) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public BaseResult apply(ApplyParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String userId = (String) request.getAttribute("userId");
		try {
			boolean flag = userServiceImpl.apply(userId);
			if (flag) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}
}
